package com.zhang.servicepassengeruser.service.impl;

import com.zhang.internalcommon.constant.CommonStatusEnum;
import com.zhang.internalcommon.dto.PassengerUser;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.servicepassengeruser.mapper.PassengerUserMapper;
import com.zhang.servicepassengeruser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private PassengerUserMapper passengerUserMapper;

	@Override
	public ResponseResult loginOrRegister(String passengerPhone) {
		// 根据手机号查询用户
		HashMap<String, Object> map = new HashMap<>();
		map.put("passenger_phone", passengerPhone);
		List<PassengerUser> passengerUsers = passengerUserMapper.selectByMap(map);

		// 判断用户信息是否存在
		if (passengerUsers.size() == 0) {
			PassengerUser passengerUser = new PassengerUser();
			passengerUser.setPassengerName("张三");
			passengerUser.setPassengerPhone(passengerPhone);
			passengerUser.setPassengerGender((byte) 0);
			passengerUser.setState((byte) 0);
			passengerUser.setGmtCreate(LocalDateTime.now());
			passengerUser.setGmtModified(LocalDateTime.now());
			passengerUserMapper.insert(passengerUser);
		}
		return ResponseResult.success();
	}

	@Override
	public ResponseResult getUserByPhone(String passengerPhone) {
		// 根据手机号查询用户
		HashMap<String, Object> map = new HashMap<>();
		map.put("passenger_phone", passengerPhone);
		List<PassengerUser> passengerUsers = passengerUserMapper.selectByMap(map);
		if (passengerUsers.isEmpty()) {
			return ResponseResult.fail(CommonStatusEnum.USER_NOT_EXISTS.getCode(), CommonStatusEnum.USER_NOT_EXISTS.getValue());
		}
		PassengerUser passengerUser = passengerUsers.get(0);
		return ResponseResult.success(passengerUser);
	}
}
